﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

namespace WebApplication1.InfoManagement
{
    public partial class StockWarnAdd : System.Web.UI.Page
    {
        private IList<StockWarnInfo> list { get { return (IList<StockWarnInfo>)ViewState["StockWarnList"]; } set { ViewState["StockWarnList"] = value; } }
        private StockWarn sw;

        public StockWarnAdd()
        {
            sw = new StockWarn();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                list = new List<StockWarnInfo>();
            }
        }

        protected void Btn_Add_Click(object sender, EventArgs e)
        {
            listAdd();
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }

        private void listAdd()
        {
            StockWarnInfo swi = new StockWarnInfo
            {
                ID = 0,
                Tool_Code = this.Tb_ToolCode.Text.Trim(),
                Amount_Warn = int.Parse(this.Tb_Amount_Warn.Text.Trim()),
            };
            if(list.Count>0)
            {
                foreach (StockWarnInfo swinfo in list)
                {
                    if (swinfo.Tool_Code.Equals(swi.Tool_Code, StringComparison.CurrentCultureIgnoreCase))
                    {
                        string str = "要添加的代码已在列表中，无法再添加！";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
                        return;
                    }
                }
            }
            list.Add(swi);
        }

        protected void Btn_Insert_Click(object sender, EventArgs e)
        {
            listAdd();
            foreach (StockWarnInfo swinfo in list)
            {
                sw.Insert(swinfo);
            }
            string str = "设置成功！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            Response.Redirect("~/InfoManagement/StockWarnManagement.aspx");
        }

        protected void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InfoManagement/StockWarnManagement.aspx");
        }
    }
}
